<template>
	<view v-if="goods_info.goods_name" class="goods_details">
		<swiper class="goods_details__swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1500" :circular="true">
			<swiper-item class="swiper_item" v-for="(item, index) in goods_info.pics" :key="index">
				<image class="swiper_item__img" :src="item.pics_big" @click="preView(index)"></image>
			</swiper-item>
		</swiper>
		<view class="goods_info_container">
			<view class="goods_price">￥{{goods_info.goods_price}}</view>
			<view class="goods_detail_box">
				<view class="goods_name">{{goods_info.goods_name}}</view>
				<view class="goods_Collection">
					<uni-icons class="goods_Collection__icon" type="star" size="16"></uni-icons>
					<text class="goods_Collection__text">收藏</text>
				</view>
			</view>
			<view class="goods_transport">快递：{{cart.length}}</view>
		</view>
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<uni-goods-nav class="goods_nav" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClickGoods" @buttonClick="buttonClickGoods" />
	</view>
</template>

<script>
	import { connnectApi } from '../../static/api'
	import { showMsg } from '../../static/common'
	import { mapState, mapMutations, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				goods_info: {},
				// 左侧按钮组的配置对象
				options: [
					{
				    icon: 'shop',
				    text: '店铺'
				  },
					{
				    icon: 'cart',
				    text: '购物车',
				    info: 0
				  }],
				  // 右侧按钮组的配置对象
				buttonGroup: [
					{
				    text: '加入购物车',
				    backgroundColor: '#ff0000',
				    color: '#fff'
				  },
					{
				    text: '立即购买',
				    backgroundColor: '#ffa200',
				    color: '#fff'
					}],
			};
		},
		
		computed: {
			//调用mapStatu方法，把my_cart模块中的cart数组映射到当前页面，作为计算属性来使用，
			//...mapState('模块的名称', ['要映射的数据名称1', '要映射的数据名称2'])
			...mapState('my_cart', ['cart']),
			...mapGetters('my_cart', ['total']),
		},
		watch: {
			total: {
				handler(newVal) {
					//获取到购物车按钮的配置对象
					const findResult = this.options.find((item) => item.text === '购物车');
					//动态为购物车按钮info属性赋值
					if(findResult) {
						findResult.info = newVal;
					}
				},
				immediate: true,	//声明此侦听器，是否在页面初次加载完毕后立即调用
			},
			
		},
		onLoad(options) {
			this.getGoodsDetail(options);
		},

		methods: {
			...mapMutations('my_cart', ['addToCart']),
			async getGoodsDetail(goods_id) {
				connnectApi.getGoodsDetail(goods_id)
				.then(({ data }) => {
					if(data.meta.status !== 200) return showMsg.showErrMsg();
					data.message.goods_introduce = data.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg');
					this.goods_info = data.message;
				})
				.catch((err) => {
					console.log(err);
				});
			},
			preView(currentIndex) {
				uni.previewImage({
					current: currentIndex,
					urls: this.goods_info.pics.map((item) => item.pics_big),
				});
			},
			
			onClickGoods(aim) {
				if(aim.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			
			buttonClickGoods(e) {
				const {goods_id, goods_name, goods_price, goods_small_logo, goods_state} = this.goods_info;
				if(e.content.text === '加入购物车') {
					const goods = {
						goods_id, 
						goods_name, 
						goods_price, 
						goods_small_logo, 
						goods_count: 1,
						goods_state
					};
					this.addToCart(goods);
				}
			},
			
		},
	}
</script>

<style lang="scss">
.goods_details {
	.goods_details__swiper {
		height: 750rpx;
		.swiper_item {
			.swiper_item__img {
				width: 100%;
				height: 100%;
			}
		}
	}
	.goods_info_container {
		padding: 10rpx;
		.goods_price {
			font-size: 40rpx;
			font-weight: 700;
			color: #c00000;
			margin: 10rpx 0;
		}
		.goods_detail_box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			.goods_name {
				font-size: 12rpx;
				padding-right: 30rpx;
			}
			.goods_Collection {
				display: flex;
				flex-direction: column;
				justify-content: center;
				font-size: 12rpx;
				border-left: 1px solid #efefef;
				color: gray;
				.goods_Collection__icon {
					
				}
				.goods_Collection__text {
					
				}
			}
		}
		.goods_transport {
			font-size: 12px;
			color: gray;
		}
		
	}
	.goods_nav {
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100%;
		}
}
</style>
